package net.sk.china.city.service.impl;

import java.time.LocalDate;
import java.util.List;

import net.sk.china.city.domain.ArArticleContent;
import net.sk.china.city.domain.vo.AddArticle;
import net.sk.china.city.domain.vo.ArticleVO;
import net.sk.china.city.domain.vo.UpdateArticle;
import net.sk.china.city.mapper.ArArticleContentMapper;
import net.sk.china.city.utils.DateUtils;
import net.sk.china.city.utils.SecurityUtils;
import net.sk.china.common.exception.ServiceException;
import net.sk.china.city.utils.PlatformIdHolder;
import net.sk.china.city.service.IArArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import net.sk.china.city.mapper.ArArticleMapper;
import net.sk.china.city.domain.ArArticle;
import org.springframework.transaction.annotation.Transactional;

/**
 * 文章管理Service业务层处理
 * 
 * @author gxf
 * @date 2024-03-27
 */
@Service
public class ArArticleServiceImpl implements IArArticleService
{
    @Autowired
    private ArArticleMapper arArticleMapper;

    @Autowired
    private ArArticleContentMapper articleContentMapper;

    /**
     * 查询文章管理
     * 
     * @param id 文章管理主键
     * @return 文章管理
     */
    @Override
    public ArticleVO selectArArticleById(Long id)
    {
        ArArticle article = arArticleMapper.selectArArticleById(id);
        ArArticleContent arArticleContent = articleContentMapper.selectById(article.getContentId());
        ArticleVO articleVO = new ArticleVO();
        articleVO.setId(article.getId());
        articleVO.setIndexImg(article.getIndexImg());
        articleVO.setArtName(article.getArtName());
        articleVO.setAuthor(article.getAuthor());
        articleVO.setCategoryId(article.getCategoryId());
        articleVO.setArticleAbstract(article.getArticleAbstract());
        articleVO.setContentId(article.getContentId());
        articleVO.setReleaseTime(article.getReleaseTime());
        articleVO.setRemark(article.getRemark());
        articleVO.setContent(arArticleContent.getContent());
        articleVO.setArticleStatus(article.getArticleStatus());
        articleVO.setArtName(article.getArtName());
        articleVO.setSource(article.getSource());
        return articleVO;
    }

    /**
     * 查询文章管理列表
     * 
     * @param arArticle 文章管理
     * @return 文章管理
     */
    @Override
    public List<ArArticle> selectArArticleList(ArArticle arArticle)
    {
        arArticle.setPlatformId(PlatformIdHolder.getPlatformId());
        return arArticleMapper.selectArArticleList(arArticle);
    }

    /**
     * 新增文章管理
     *
     * @return 结果
     */
    @Override
    @Transactional
    public int insertArArticle(AddArticle addArticle)
    {
        ArArticleContent  arArticleContent = new ArArticleContent();
        arArticleContent.setContent(addArticle.getContent());
        int insert = articleContentMapper.insert(arArticleContent);
        if(insert <=0 ){
            throw new ServiceException("新增失败",409);
        }
        ArArticle arArticle = new ArArticle();
        arArticle.setArticleStatus(addArticle.getArticleStatus());
        arArticle.setIndexImg(addArticle.getIndexImg());
        arArticle.setContentId(arArticleContent.getId());
        arArticle.setCategoryId(addArticle.getCategoryId());
        arArticle.setArtName(addArticle.getArtName());
        arArticle.setAuthor(addArticle.getAuthor());
        arArticle.setArticleAbstract(addArticle.getArticleAbstract());
        arArticle.setCreateTime(LocalDate.now());
        arArticle.setCreateBy(SecurityUtils.getUsername());
        arArticle.setRemark(addArticle.getRemark());
        arArticle.setSource(addArticle.getSource());
        arArticle.setReleaseTime(addArticle.getReleaseTime());
        arArticle.setPlatformId(PlatformIdHolder.getPlatformId());
        int i = arArticleMapper.insertArArticle(arArticle);
        if(i <=0 ){
            throw new ServiceException("新增失败",409);
        }
        return 1;
    }

    /**
     * 修改文章管理
     *
     * @return 结果
     */
    @Override
    @Transactional
    public int updateArArticle(UpdateArticle updateArticle)
    {
        ArArticle arArticle = new ArArticle();
        arArticle.setId(updateArticle.getId());
        arArticle.setIndexImg(updateArticle.getIndexImg());
        arArticle.setArtName(updateArticle.getArtName());
        arArticle.setAuthor(updateArticle.getAuthor());
        arArticle.setCategoryId(updateArticle.getCategoryId());
        arArticle.setArticleAbstract(updateArticle.getArticleAbstract());
        arArticle.setArticleStatus(updateArticle.getArticleStatus());
        arArticle.setRemark(updateArticle.getRemark());
        arArticle.setReleaseTime(updateArticle.getReleaseTime());
        arArticle.setSource(updateArticle.getSource());
        arArticle.setUpdateTime(LocalDate.now());
        arArticle.setUpdateBy(SecurityUtils.getUsername());
        int i = arArticleMapper.updateArArticle(arArticle);
        if(i <=0 ){
            throw new ServiceException("修改失败",409);
        }
        ArArticleContent arArticleContent = new ArArticleContent();
        arArticleContent.setId(updateArticle.getContentId());
        arArticleContent.setContent(updateArticle.getContent());
        int i1 = articleContentMapper.updateById(arArticleContent);
        if(i1 <=0 ){
            throw new ServiceException("修改失败",409);
        }
        return 1;
    }

    /**
     * 批量删除文章管理
     * 
     * @param ids 需要删除的文章管理主键
     * @return 结果
     */
    @Override
    public int deleteArArticleByIds(Long[] ids)
    {
        return arArticleMapper.deleteArArticleByIds(ids);
    }

    /**
     * 删除文章管理信息
     * 
     * @param id 文章管理主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteArArticleById(Long id)
    {
        int i = articleContentMapper.deleteById(arArticleMapper.selectArArticleById(id).getContentId());
        if(i <=0 ){
            throw new ServiceException("删除失败",409);
        }
        int i1 = arArticleMapper.deleteArArticleById(id);
        if(i1 <=0 ){
            throw new ServiceException("删除失败",409);
        }
        return 1;
    }

    @Override
    public int updateStatus(ArArticle arArticle){
        ArArticle article = new ArArticle();
        article.setId(arArticle.getId());
        article.setArticleStatus(arArticle.getArticleStatus());
        article.setUpdateTime(LocalDate.now());
        article.setUpdateBy(SecurityUtils.getUsername());
        int i = arArticleMapper.updateArArticle(article);
        if(i <=0 ){
            throw new ServiceException("修改失败",409);
        }
        return i;
    }
}
